package com.briup.day07;

import java.util.Arrays;

/**
 * @author ThinkBook
 */
public class Test04_Basic {
//      1.封装一个方法，获取数组平均值  getAvg(int[] arr)
    public static void getAvg(int[] arr){
        double sum = 0;
        int count = arr.length;
        for (int i = 0;i<arr.length;i++){
            sum +=arr[i];

        }
        double avg = sum/count;
        System.out.println(avg);
    }
//      2.封装一个方法，获取数组最大值	getMax(int[] arr)
    public static void getMax(int[] arr){
        int max = arr[0];
        for (int i = 0;i<arr.length;i++){
            if(arr[i] > max){
                max = arr[i];
            }
        }
        System.out.println(max);
    }
//      3.封装一个方法，将数组元素反转  reverse(int[] arr)
    public static void reverse(int[] arr){
        int len = arr.length;
        for(int i = 0; i < len/2; i++) {
//      交换 arr[i] 和 arr[len-1-i]的值
            arr[i] = arr[i] ^ arr[len-1-i];
            arr[len-1-i] = arr[i] ^ arr[len-1-i];
            arr[i] = arr[i] ^ arr[len-1-i];
        }
    }
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4};
        getAvg(arr1);
        getMax(arr1);
        reverse(arr1);
        System.out.println(Arrays.toString(arr1));

    }
//    遍历数组
    public static void outArray(int[] arr){
        for (int i=0;i<arr.length;i++){
            System.out.print(arr[i] + "\t");
        }
        System.out.println();
    }

    public static void main04(String[] args) {
        int[] arr1 = {1,2,3,4};
        int[] arr2 = arr1;

//        遍历数组
        outArray(arr1);
//修改arr1元素，arr2会收到影响：arr1和arr2标识同一块内存
        arr1[2] = 8;
        outArray(arr2);

        arr2[0] = 5;
        outArray(arr1);
    }
    //静态初始化
    public static void main03(String[] args) {
        int[] arr =new int[]{3,4,2,5,1};

        for(int i = 0; i < arr.length; i++) {
            System.out.println("arr[" + i + "]: " + arr[i]);
        }
            System.out.println("----------------");
            double[] arr2 = {3.13,2.3,-5.6,4.1};
            for(int j = 0; j < arr2.length; j++) {
                System.out.println("arr[" + j + "]: " + arr2[j]);
        }
    }
    public static void main02(String[] args) {
        int[] arr = new int[4];
//        [I@14ae5a5
        /*
        * [          代表一维数组
        * I          数组元素int类型
        * @          分隔符
        * 14ae5a5    地址值16进制
        */
        System.out.println("arr:"+arr);

        double[] arr2 = new double[3];
//        [D@7f31245a
        System.out.println("arr2:"+arr2);
    }
    public static void main01(String[] args) {
        System.out.println("数组长度: " + args.length);

        if(args.length > 0) {
            System.out.println("args[0]: " + args[0]);
        }
    }
}
